// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  if (event.name1) {
    await db.collection('gods').where({
      _openid: wxContext.OPENID,
    }).update({
      data: {
        btList: _.push({
          name: "发布表白墙",
          time: new Date(),
          num: 5
        })
      }
    })
    await db.collection('love').add({
      data: {
        name1: event.name1,
        name2: event.name2,
        text: event.text,
        _openid: wxContext.OPENID,
        time: new Date(),
        like: 0,
        comment: []
      }
    })
    return 200
  } else if (event.skip || event.skip == 0) {
    return db.collection('love').aggregate().sort({
      time: -1
    }).skip(event.skip).limit(10).end()
  } else {
    for (const index in event.list) {
      const element = event.list[index]
      await db.collection('love').doc(element._id).update({
        data: {
          like: element.like,
          comment: element.comment
        },
      })
    }
  }
  return "ok"
}